//公共实例
var S4 = function () {
  return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
var Guid = function () {
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
var common = {
  placeInstance: {
    //获取当前位置实例
    getPlaceInstance: function (id, list) {
      var res = null;
      for (var i = 0; i < list.length; i++) {
        if (id == list[i].id) {
          res = list[i];
          break;
        }
      }
      return res;
    },

  },
  monsterInstance: {
    //解决一次刷多只怪，重叠出现的问题
    resolveOverlay: function (arr) {
      var _list = arr;
      for (var j = 1; j < 6; j++) {
        var _top = 0;
        for (var z = 0; z < _list.length; z++) {
          //外层循环队列数，内层怪数组循环在这个对列的怪，top持续累加
          if (_list[z].trackNum == j) {
            _list[z].top = _top;
            _top += 15;
          }
        }
      }
      return _list;
    },
    //根据guid获取怪物实例
    getInstanceFromMonsterList: function (guid, _tempList) {
      var res = null;
      for (var i = 0; i < _tempList.length; i++) {
        if (guid == _tempList[i].guid) {
          res = _tempList[i];
          break;
        }
      }
      return res;
    },

    //从当前难度怪物列表中随机取出N个实例
    getRandomMonsterArr: function (n, commonData,hardType) {
      var res = [];
      var _count = n;
      while (_count > 0) {
        //取从0-n之间的随机整数,并且取出怪物数组中随机一个
        var tempIdx = parseInt(Math.random() * (commonData.monsters.length));
        var monster = commonData.monsters[tempIdx];
        var newObj = {};
        newObj.id = monster.id;
        newObj.imgUrl = monster.imgUrl;
        newObj.name = monster.name;
        newObj.ap = monster.ap;
        newObj.speed = monster.speed;
        newObj.moveCdTime = monster.moveCdTime;
        newObj.width = monster.width;
        newObj.height = monster.height;
        //根据难度，调整怪的血量，提供得分，猫晶
        switch(hardType)
        {
          case 1:
            newObj.hp = monster.hp;
            newObj.soure = monster.soure;
            newObj.price = monster.price;
            break;
          case 2:
            newObj.hp = monster.hp*2;
            newObj.soure = monster.soure*2;
            newObj.price = monster.price*2;
            break;
          case 3:
            newObj.hp = monster.hp * 3;
            newObj.soure = monster.soure * 3;
            newObj.price = monster.price * 3;
          break;
        }
        
        newObj.top = monster.top;
        newObj.trackNum = monster.trackNum;
        newObj.power = monster.power;
      
        newObj.isDead = false;
        newObj.isShowHart = false;//是否显示怪收到的伤害
        newObj.hart = 0;//伤害值
        newObj.guid = Guid();//给怪对象建立唯一标识
        
        res.push(newObj);
        _count--;
      }
      return res;
    },
    //获取指定序列的怪,按从近到远排序
    getMonstersByTrackNum:function(trackNum,monsterList){
        var res=[];
        for (var i = 0; i < monsterList.length;i++){
          if (monsterList[i].trackNum == trackNum ){
            res.push(monsterList[i]);
          }
        }
        res.sort(function (x, y) {
          return x.top > y.top ? 1 : -1;
        });
        return res;
    }
  }
}
module.exports = {
  common: common,
  guid: Guid
}